﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Dapper;
using hello.csharp.common.Interfaces;
using hello.csharp.common.Interfaces.Storages;
using hello.csharp.domain;
using hello.csharp.helper;
using hello.csharp.helper.Enums;
using NLog;

namespace hello.csharp.storage
{
    public class UserSnackStorageWrite : IUserSnackStorageWrite
    {
        private readonly Logger _logger = LogManager.GetCurrentClassLogger();
        private readonly IDbManager _dbManager;
        private readonly SqlBox _sqlBox;

        public UserSnackStorageWrite(IDbManager dbManager, IDbInitialise dbInit)
        {
            _dbManager = dbManager;
#if DEBUG
            var dbType = DatabaseType.SqLite;
#else
            var dbType = DatabaseType.PostgreSql;
#endif
            _sqlBox = dbInit.SqlSets[dbType];
        }

        /// <summary>
        /// 当前实现面向的数据表表名
        /// </summary>
        public string TableName { get; } = $"{nameof(UserSnack)}s";

        /// <summary>
        /// 将指定的对象插入数据库中
        /// </summary>
        /// <param name="userSnack">指定的对象</param>
        public async Task InsertAsync(UserSnack userSnack)
        {
            var conn = _dbManager.OpenWriteConnection();
            var sql = _sqlBox.Insert[nameof(UserSnack)];
            await conn.ExecuteAsync(sql, userSnack);
        }

        /// <summary>
        /// 更新指定的对象
        /// </summary>
        /// <param name="obj">指定的对象</param>
        public async Task UpdateAsync(UserSnack obj)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 根据记录ID，逻辑删除指定记录，即打上“已删除标记”，但并不从数据库中删除记录
        /// </summary>
        /// <param name="id">指定的记录ID</param>
        public async Task LogicDeleteAsync(string id)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 根据记录ID，从数据库中移除该记录，该记录被移除后，不可恢复
        /// </summary>
        /// <param name="id">指定的记录ID</param>
        public async Task RemoveAsync(string id)
        {
            throw new NotImplementedException();
        }

        public async Task InsertManyAsync(UserSnack[] userSnacks)
        {
            var conn = _dbManager.OpenWriteConnection();
            var sql = _sqlBox.Insert[nameof(UserSnack)];
            await conn.ExecuteAsync(sql, userSnacks);
        }
    }
}
